/**
 * 宽带预约列表
 */
(function($) {
  var pageData = {};
  var pageCount = 1;
  var filterType = 3;
  var pageNumber = 0;
  var custPageNumber = 0;
  var groupList = new Array();
  var custList = new Array();
  var smsInterval;
  var second = 60;
  /*
  封装参数
  */
  var getMyParams = function(phone) {
    var params = "?";
    var myParam = getParams();
    if (myParam) {
      params += myParam;
      if (phone) {
        params += "&";
      }
    }
    if (phone) {
      params += "phone=" + phone;
    }

    return params;
  };

  var isDigital = function(keyword) {
    var reg = /^[0-9]*$/;
    if (reg.test(keyword)) {
      return true;
    }
    return false;
  }

  var invalidParam = function(val, errorMsg) {
    console.log("val=" + val + ",msg=" + errorMsg);
    if (val) {
      return false;
    } else {
      showAlertMessage(errorMsg);
      return true;
    }
  }

  var smsDisabledInterval = function() {
    if (second > 1) {
      $("#send_sms_btn").html("重新发送(" +second+ ")");
      second--;
    } else {
     clearSmsInterval();
    }
  }

  var clearSmsInterval= function(){
    clearInterval(smsInterval);
    $("#send_sms_btn").removeClass('am-disabled');
    $("#send_sms_btn").html('发送验证码');
  }

  var sendSmsCode = function() {
    var phoneNumber = $("#phone").val();
    console.log("phoneNumber=" + phoneNumber);
    if (!phoneNumber || isNaN(phoneNumber) || phoneNumber.length != 11) {
      showAlertMessage("请填写正确的手机号码");
      return;
    }
    $("#send_sms_btn").addClass('am-disabled');
    second = 60;
    $("#send_sms_btn").html("重新发送(" +second+ ")");
    smsInterval = setInterval(smsDisabledInterval, 1000);
    $('#modal').modal('open');
    sendSms(phoneNumber);
  }

  /*
发送验证码
*/
  var sendSms = function(tel) {
    // var params = {
    //   phoneNumber: tel,
    //   empCode:getParamValue("empCode"),
    //   token:getParamValue("token")
    // };
    // var url = "../../../govBroadbandH5/getVerifyCode";
    var url = Constants.apiV2Host + 'sms/send?phoneNumber=' + tel + '&smsType=9' + '&empCode=' + getParamValue('EmpCode') + '&token=' + getParamValue('token');
    $.ajax({
      type: "get",
      url: url,
      // data: params,
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        $('#modal').modal('close');
        if (data && data.ResultCode != 1000) {
          showAlertMessage("发送验证码失败！" + data.Message);
          clearSmsInterval();
          return;
        }
        showAlertMessage("发送验证码成功！");
      },
      error: function(textStatus) {
        $('#modal').modal('close');
        showAlertMessage("发送验证码失败！");
        clearSmsInterval();
      }
    });
  };


  /*
  验证验证码
  */
  var verifySms = function(tel, code, callback) {
    // var params = {
    //   phoneNo: tel,
    //   verifyCode: code,
    //   empCode:getParamValue("empCode"),
    //   token:getParamValue("token")
    // };
    // var url = "../../../govBroadbandH5/verifySms";
    var url = Constants.apiV2Host + 'sms/validate?phoneNumber=' + tel + '&smsCode=' + code + '&empCode=' + getParamValue('EmpCode') + '&smsType=9&token=' + getParamValue('token');
    $.ajax({
      type: "get",
      url: url,
      // data: params,
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        $('#modal').modal('close');
        console.info(data);
        if (data && data.ResultCode != 1000) {
          showAlertMessage("短信验证码校验失败！" + data.Message);
          return;
        }
        callback();
      },
      error: function(textStatus) {
        $('#modal').modal('close');
        showAlertMessage("短信验证码校验失败！");
      }
    });
  };

  var showAlertMessage = function(message) {
    $("#vgroup-alert-content").html(message);
    $("#vgroup-alert").modal('open');
  }

  var closeAlertMessage = function() {
    $("#vgroup-alert").modal('close');
  }

  var smsVerifyCB = function() {
    console.log("短信验证码正确，跳转到资费详情页面");
    var phoneNumber = $("#phone").val();
    window.location.href = "vgroup_zfbg_detail.html" + getMyParams(phoneNumber);
  }

  $(function() {
    $("#send_sms_btn").on("click", function() {
      sendSmsCode();
    });

    $("#submit").on("click", function() {
      var phoneNumber = $("#phone").val();
      if (!phoneNumber || isNaN(phoneNumber) || phoneNumber.length != 11) {
        showAlertMessage("请填写正确的手机号码");
        return;
      }
      var smsCode = $("#sms_code").val();
      if (!smsCode || isNaN(smsCode) || smsCode.length != 6) {
        showAlertMessage("请填写正确的短信验证码");
        return;
      }
      verifySms(phoneNumber, smsCode, smsVerifyCB);
    });
  });
})(jQuery);